package com.cvte.shop.interceptor;

import com.cvte.shop.constants.UserConstants;
import com.cvte.shop.entity.ShopUserAll;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author DaiShubo
 * @version 1.0
 * @description 根据用户等级查看是否可以
 * @createTime 2020/7/27 8:07 下午
 * @since 1.7
 **/
@Component
public class UserGradeInterceptor implements HandlerInterceptor {



    public boolean preHandle(HttpServletRequest httpSersvletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        HttpSession session = httpSersvletRequest.getSession();
        ShopUserAll userAll = (ShopUserAll)session.getAttribute(UserConstants.USER_TOKEN);
        if(userAll == null){
            httpServletResponse.sendRedirect("/login/un_login");
            return false;
        }
        Integer grade = userAll.getGrade();
        if(grade >= UserConstants.INTERCEPTOR_URL_USER_CODE){
            return true;
        }else{
            httpServletResponse.sendRedirect("/resources/forbidden");
            return false;
        }
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
